package com.jiuyue.movehouse.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiuyue.movehouse.common.exception.BadRequestException;
import com.jiuyue.movehouse.entity.User;
import com.jiuyue.movehouse.mapper.UserMapper;
import com.jiuyue.movehouse.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yourFather
 * @since 2022-02-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User Login(User user) {
        User old = this.getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername,user.getUsername())
                .eq(User::getPassword,user.getPassword())
        );
        if (Objects.isNull(old)){
            throw new BadRequestException("用户名或密码错误");
        }
        if (user.getType().equals(1)&&!old.getType().equals(1)){
            throw new BadRequestException("非管理员账号");
        }
        return old;
    }

    @Override
    public User Register(User user) {
        checkUserRepeat(user);
        user.setType(0);
        this.save(user);
        return user;
    }

    @Override
    public Page<User> getUserList(Page page, User user) {
        Page<User> userPage = this.page(page,Wrappers.<User>lambdaQuery()
                .eq(StrUtil.isNotEmpty(user.getUsername()),User::getUsername,user.getUsername())
                .eq(Objects.nonNull(user.getType()),User::getType,user.getType())
        );
        return userPage;
    }

    @Override
    public void resetPassword(Long userId, String password) {
        this.update(Wrappers.<User>lambdaUpdate().eq(User::getId,userId).set(User::getPassword,password));
    }

    @Override
    public void deleteUser(Long userId) {
        if (userId.equals(1L)){
            throw new BadRequestException("默认管理员账号不能删除");
        }

        User user = this.getById(userId);
        if (user.getHaveOrder().equals(1)){
            throw new BadRequestException(user.getUsername()+"存在订单,不能删除");
        }
        this.removeById(userId);
    }

    @Override
    public void updateType(Long userId, String type) {
        if (userId.equals(1L)){
            throw new BadRequestException("默认管理员账号不能修改");
        }

        this.update(Wrappers.<User>lambdaUpdate().eq(User::getId,userId).set(User::getType,type));
    }

    @Override
    public User getByUserId(Long userId) {
        return baseMapper.getByUserId(userId);
    }

    private void checkUserRepeat(User user){
        if (this.count(Wrappers.<User>lambdaQuery().eq(User::getUsername,user.getUsername()))>0){
            throw new BadRequestException("用户名已存在");
        }

        if (this.count(Wrappers.<User>lambdaQuery().eq(User::getIdCard,user.getIdCard()))>0){
            throw new BadRequestException("该身份证已注册");
        }

        if (this.count(Wrappers.<User>lambdaQuery().eq(User::getPhone,user.getPhone()))>0){
            throw new BadRequestException("该身份证已注册");
        }
    }
}
